<?php

class Ratings_Model extends Model {

    function __construct() {
        parent::__construct();
    }

    /**
     * Print Highscore table 
     */
    function PrintHighScoreTable() {
        $sth = $this->database->prepare("SELECT Members.username AS username, Ratings.rating AS rating FROM Members INNER JOIN Ratings ON Members.memberId = Ratings.memberId ORDER BY Ratings.rating DESC LIMIT 0, 20");
        $sth->execute();

        return $sth;
    }


    function AddMatchResult($teamAPlayer1, $teamAPlayer2, $teamBPlayer1, $teamBPlayer2, $teamAScore, $teamBScore) {

        $sth = $this->database->prepare("INSERT INTO Matchs (teamAPlayer1, teamAPlayer2, teamBPlayer1, teamBPlayer2, teamAScore, teamBScore) VALUES (:teamAPlayer1, :teamAPlayer2, :teamBPlayer1, :teamBPlayer2, :teamAScore, :teamBScore)");
        $sth->execute(array(
            ':teamAPlayer1' => $teamAPlayer1,
            ':teamAPlayer2' => $teamAPlayer2,
            ':teamBPlayer1' => $teamBPlayer1,
            ':teamBPlayer2' => $teamBPlayer2,
            ':teamAScore' => $teamAScore,
            ':teamBScore' => $teamBScore,
        ));               
    }

    function CalcRatingChange($Player1TeamARating, $Player2TeamARating, $Player1TeamBRating, $Player2TeamBRating, $ScoreTeamA, $ScoreTeamB, $FactorK, $ScoreMagnitude)
    {
        $TeamARating = ($Player1TeamARating + $Player2TeamARating) / 2;
        $TeamBRating = ($Player1TeamBRating + $Player2TeamBRating) / 2;

        $RatingDiff = $TeamARating - $TeamBRating;

        $We = 1/(1 + pow(10, -$RatingDiff / $ScoreMagnitude));
               
        if($ScoreTeamA > $ScoreTeamB)
            $Score = 1;
        if($ScoreTeamA < $ScoreTeamB)
            $Score = -1;
        if($ScoreTeamA == $ScoreTeamB)
            $Score = 0;
        
        $RatingChange = round($FactorK*($Score - $We),0);            
        
        return $RatingChange;
    }

    function GetRating($memberId) {
        $sth = $this->database->prepare("SELECT rating FROM Ratings WHERE memberId=:memberId");
        $sth->execute(array(
            ':memberId' => $memberId,
        ));
        
        $rating = $sth->fetchColumn();
        
        return $rating;      
        
    }

    function AddRating($memberId, $rating) {
        //Add users rating to Rating table
        $sth = $this->database->prepare("INSERT INTO Ratings (memberId, rating) VALUES (:memberId, :rating)");
        $sth->execute(array(
            ':memberId' => $memberId,
            ':rating' => $rating,
        ));
    }
    
    function ChangeRating($memberId, $rating) {
        //Update users rating
        $sth = $this->database->prepare("UPDATE Ratings SET rating=:rating WHERE memberId=:memberId");
        $sth->execute(array(
            ':memberId' => $memberId,
            ':rating' => $rating,
        ));
    }      
        
    function AddPassiveUser($username, $rating) {
        //Add User to Members table
        $sth = $this->database->prepare("INSERT INTO Members (username, role) VALUES (:username, 'passive')");
        $sth->execute(array(
            ':username' => $username,
        ));
        //Get memberId
        $memberId = $this->GetMemberId($username);
        
        //Add RatingScore for memberId
        $this->AddRating($memberId, $rating);

    }
    
    function GetMemberId($username) {
        //Get memberID from Members
        $sth = $this->database->prepare("SELECT memberId FROM Members WHERE username=:username");
        $sth->execute(array(
            ':username' => $username,
        ));
        
        $memberId = $sth->fetchColumn();
        
        return $memberId;
    }
 
    
    
}

?>
